import { get } from '../api/getData';
import Share from './Share';
import wechat from '../config/wechat';
import {getQueryString} from './util';

let wxUtils = {};

//////////////////////////////////////////////////////////////////////////////////////
//
// 分享
//
//////////////////////////////////////////////////////////////////////////////////////

/**
 getshareinfo?type=
 type :goods 课程详情 team 团详情
 id 课程id
 tid 团ID
 * @param config
 * @param shareInfo {imgUrl,title,description,link}
 */

function share2wx(config, shareInfo) {
    const share = new Share({
        appid: config.appId, // 必填，公众号的唯一标识
        timestamp: config.timestamp, // 必填，生成签名的时间戳
        nonceStr: config.nonceStr, // 必填，生成签名的随机串
        signature: config.signature, // 必填，签名
    });
    share.init(Object.assign({}, shareInfo));
}

function getConfig(shareInfo) {
    let url = '/api/wechat/open-platform/jssdk/'+ getQueryString('wid');
    get(url , {url : window.location.href} , function (error , data) {
        if(error === null) {
            if(data.error === undefined) {
                share2wx(JSON.parse(data.data), shareInfo);
            }
        }
    });
}

/**
 * @param shareInfo
 */

wxUtils.share = function (shareInfo) {
    getConfig(shareInfo);
};

//////////////////////////////////////////////////////////////////////////////////////
//
// 分享结束
//
//////////////////////////////////////////////////////////////////////////////////////

/**
 * 是否开启右上角Menu
 * @param open
 */

wxUtils.optionMenu = function (open = true) {
    if (open) {
        openOptionMenu();
    } else {
        disabledOptionMenu();
    }
};

/**
 * 是否禁用右上角
 */

function disabledOptionMenu() {
    if (typeof WeixinJSBridge === "undefined") {
        if (document.addEventListener) {
            document.addEventListener('WeixinJSBridgeReady', onBridgeReady(true), false);
        } else if (document.attachEvent) {
            document.attachEvent('WeixinJSBridgeReady', onBridgeReady(true));
            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady(true));
        }
    } else {
        onBridgeReady(true);
    }
}

/**
 * 开启menu
 */

function openOptionMenu() {
    if (typeof window.WeixinJSBridge === "undefined") {
        if (document.addEventListener) {
            document.addEventListener('WeixinJSBridgeReady', onBridgeReady(false), false);
        } else if (document.attachEvent) {
            document.attachEvent('WeixinJSBridgeReady', onBridgeReady(false));
            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady(false));
        }
    } else {
        onBridgeReady(false);
    }
}

function onBridgeReady(disable = true) {
    console.log(window.WeixinJSBridge);
    if (typeof window.WeixinJSBridge !== "undefined") window.WeixinJSBridge.call(disable ? 'hideOptionMenu' : 'showOptionMenu');
}
/**
 * 隐藏微信网页底部的导航栏
 * @param disable
 */

wxUtils.disabledToolbar = function (disable = true) {
    document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
        // 通过下面这个API隐藏底部导航栏
        window.WeixinJSBridge.on(disable ? 'hideToolbar' : 'showToolbar');
    });
};

export default wxUtils;
